// dingtalk.js
const axios = require('axios');
const crypto = require('crypto');
const LogUtil = require("./log_util");

const dingtalkWebhook = 'https://oapi.dingtalk.com/robot/send?access_token=d34583194d9fc3efac03fef801ecd552df8669643521d5009b55b40c8e430ade';
const secret = 'SECe81e821019331f3db62456f4bc0ebd6451df2441148c78272ba87b24255215bb'; // 你的钉钉机器人的 Secret

function sendDingtalkMessage(content) {
    const message = {
        "msgtype": "text",
        "text": {
            "content": content
        }
    };

    const timestamp = Date.now();
    const stringToSign = `${timestamp}\n${secret}`;
    const hmac = crypto.createHmac('sha256', secret);
    hmac.update(stringToSign);
    const sign = encodeURIComponent(hmac.digest('base64'));

    const url = `${dingtalkWebhook}&timestamp=${timestamp}&sign=${sign}`;

    return axios.post(url, message)
        .then(res => {
        })
        .catch(error => {
            LogUtil.logE(`钉钉消息发送失败: ${error}`);
        });
}

module.exports = sendDingtalkMessage;